package com.yanqu.road.dao.impl.activity.tongbing.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tongbing.config.TongBingCityConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class TongBingCityConfigDaoImpl extends TempDao {

    private static String getTongBingCityConfigSql = "select * from `t_s_activity_tongbing_city` where `activityId`=?";

    public Map<Integer, TongBingCityConfig> getTongBingCityConfigMap(int activityId) {
        Map<Integer, TongBingCityConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTongBingCityConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TongBingCityConfig tongBingCityConfig = new TongBingCityConfig();
                tongBingCityConfig.setActivityId(rs.getInt("activityId"));
                tongBingCityConfig.setId(rs.getInt("id"));
                tongBingCityConfig.setName(rs.getString("name"));
                tongBingCityConfig.setMapType(rs.getInt("mapType"));
                tongBingCityConfig.setType(rs.getInt("type"));
                tongBingCityConfig.setInitialCamp(rs.getInt("initialCamp"));
                tongBingCityConfig.setShowType(rs.getInt("showType"));

                String specialAddition = rs.getString("specialAddition");
                tongBingCityConfig.setSpecialAddition(specialAddition);
                Map<Integer, Integer> specAddiMap = new ConcurrentHashMap<>();
                if (!Objects.equals("0", specialAddition)) {
                    List<Integer> list = StringUtils.stringToIntegerList(specialAddition, ";");
                    specAddiMap.put(list.get(0), list.get(1));
                }
                tongBingCityConfig.setSpecialAdditionMap(specAddiMap);

                map.put(tongBingCityConfig.getId(), tongBingCityConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
